//
//  SVGVideoElement.m
//  Shake and Bake
//
//  Created by Antoine Quint on 05/11/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "SVGElement.h"
#import "SVGImageElement.h"
#import "Canvas.h"

@implementation SVGImageElement

- (id)initWithAttributeDict:(NSDictionary *)attributesDict andView:(Canvas *)aView
{
  [super initWithAttributeDict:attributesDict andView:aView];
	if (self) {
		// parse attribute values
		x = [[attributes objectForKey:ATTRIBUTE_NAME_X] floatValue];
		y = [[attributes objectForKey:ATTRIBUTE_NAME_Y] floatValue];
		width = [[attributes objectForKey:ATTRIBUTE_NAME_WIDTH] floatValue];
		height = [[attributes objectForKey:ATTRIBUTE_NAME_HEIGHT] floatValue];
		filePath = [attributes objectForKey:[view getQNameForAttribute:ATTRIBUTE_NAME_HREF inNamespace:NS_URI_XLINK]];
    //
		self.position = CGPointMake(x, y); 
    self.bounds = CGRectMake(0.0f, 0.0f, width, height);
    //
		self.name = ELEMENT_NAME_IMAGE;
  }
  return self;
}

- (void)drawInContext:(CGContextRef)context
{ 
  NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:YES]; 
  [NSGraphicsContext saveGraphicsState]; 
  [NSGraphicsContext setCurrentContext:nsGraphicsContext];
  NSImage *image = [[NSImage alloc] initByReferencingFile:filePath];
  [image setFlipped:YES];
  [image drawInRect: NSMakeRect(0.0f, 0.0f, width, height)
           fromRect: NSZeroRect
          operation: NSCompositeSourceOver
           fraction: 1.0];
  [NSGraphicsContext restoreGraphicsState]; 
} 

@end
